﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VntNet.PowerSchemeSwitcher.Properties;

namespace VntNet.PowerSchemeSwitcher
{
    internal class AppContext:ApplicationContext
    {

        SystemTryIcon _systemTryIcon;
        PowerNotificationForm _pwrForm;
        AutoUpdatePowerSchemeEngine _autoUpdate;
        PowerSettingChangeNotification _settingsCangheNot;
        SettingsForm _settingForm;
        bool _validSettings;
        const int BALLOON_TIMEOUT = 3000;
        WindowsFormsSynchronizationContext syncContext = new WindowsFormsSynchronizationContext();

        public AppContext()
        {
            _autoUpdate = new AutoUpdatePowerSchemeEngine();
            _pwrForm = new PowerNotificationForm();
            _systemTryIcon = new SystemTryIcon();
            _systemTryIcon.ShowSettingForm += new Action(ShowAppSettingForm);
            _systemTryIcon.SettingChanged += new Action(AppSettingChanged);
            _systemTryIcon.Show();
            _pwrForm.PowerStatusChangedEvent += new Action<string, PowerSettingNotificationMsgFilter.PowerSavingLevelEnum>(PowerStatusChanged);            
            _pwrForm.Show();
            _settingsCangheNot = new PowerSettingChangeNotification();
            _settingsCangheNot.PowerSettingsChanged += new Action(PowerSettingsChanged);
            _autoUpdate.SettingValidated += new Action<bool>(AppSettingsValidated);
            _autoUpdate.Update();
            _validSettings = true;
        }

        void AppSettingsValidated(bool result)
        {
            syncContext.Post(delegate { AppSettingsValidatedInternal(result); }, null);
        }

        void AppSettingsValidatedInternal(bool result)
        {
            _validSettings = result;
            if (!result && Settings.Default.EnableAutoSwitching)
            {
                _systemTryIcon.ShowBalloonTip(
                    BALLOON_TIMEOUT,
                    "Failed to applay saved settings",
                    "The saved settings can not be used.Please open setting and fix them.Auto mode is now be disabled",
                    ToolTipIcon.Error);
                Settings.Default.EnableAutoSwitching = false;
            }
            _systemTryIcon.UpdateMenuAutoItemStatus(result);
        }

        void ShowAppSettingForm()
        {
            if (_settingForm == null)
            {
                _settingForm = new SettingsForm();
                _settingForm.SettingChanged += new Action(AppSettingChanged);
                _settingForm.FormClosed += new FormClosedEventHandler(SettingFormClosed);
                _settingForm.Show();
            }
        }

        void SettingFormClosed(object sender, FormClosedEventArgs e)
        {
            _settingForm = null;
        }

        void PowerSettingsChanged()
        {
            syncContext.Post(delegate { PowerSettingsChangedInternal(); }, null);
        }

        void PowerSettingsChangedInternal()
        {
            //update tool tip and menu items
            _systemTryIcon.UpdateUiInfo();            
            //update engine because we need to validate settings
            //and eventually stop it if the changes broke them
            _autoUpdate.Update();
        }

        void AppSettingChanged()
        {
            _autoUpdate.Update();
        }

        void PowerStatusChanged(string currentLineName, PowerSettingNotificationMsgFilter.PowerSavingLevelEnum currentPowerSavingLevel)
        {
            syncContext.Post(delegate { PowerStatusChangedInternal(currentLineName, currentPowerSavingLevel); }, null);
        }

        void PowerStatusChangedInternal(string currentLineName, PowerSettingNotificationMsgFilter.PowerSavingLevelEnum currentPowerSavingLevel)
        {            
            _systemTryIcon.UpdateUiInfo(currentLineName, currentPowerSavingLevel);
        }
    }
}
